<?php
namespace App\Http\Controllers\Admin;

use App\Models\Goods;
use App\Models\GoodsAttribute;
use App\Models\GoodsBuys;
use App\Models\GoodsEvaluate;
use App\Models\GoodsPicture;
use Illuminate\Http\Request;
use Auth;
use Illuminate\Support\Facades\Cache;

/**
 * 产品
 *
 * @author Miluo Huang
 */
class GoodsController extends Controller
{

    /**
     * 产品列表
     */
    public function showList(Request $request)
    {
        $data = Goods::orderBy('created_at', 'desc');
        if ($request->has('goods_number')) {
            $data = $data->where('goods_number', $request->input('goods_number'));
        }
        $data = $data->paginate();
        $data->appends($request->only(['goods_number']));

        return view('admin.goods.list')->withData($data)->withGet($request->all());
    }

    /**
     * 产品添加
     */
    public function showEdit(Request $request)
    {
        $data    = Goods::find($request->input('id'));
        $status  = Goods::$status;
        $is_item = Goods::$is_item;
        $is_index = Goods::$is_index;
        $top_recommended = Goods::$top_recommended;
        $show_place = Goods::$show_place;


        return view('admin.goods.edit')->withData($data)->withStatus($status)->withIsItem($is_item)->withIsIndex($is_index)->withTopRecommended($top_recommended)->withShowPlace($show_place);
    }

    /**
     * 产品添加保存
     */
    public function saveEdit(Request $request)
    {
        // 验证输入。
        $this->validate($request, [
            'name'       => 'required',
            'attribute'  => 'required',
            'details'    => 'required',
            'picture_id' => 'required',
            'cover_id'   => 'required'
        ], [
            'name.required'       => '请填写产品名称！',
            'attribute.required'  => '请填写产品参数！',
            'details.required'    => '请填写产品详情！',
            'picture_id.required' => '请填写产品图片！',
            'cover_id.required'   => '请上传封面图！',
        ]);
        $request->has('id') ? $data = Goods::find($request->input('id')) : $data = new Goods();
        $data->name    = $request->input('name');
        $data->details = $request->input('details');
        $request->has('sort_at') ? $data->sort_at = $request->input('sort_at') : '';
        $request->has('original_price') ? $data->original_price = $request->input('original_price') : '';
        $request->has('present_price') ? $data->present_price = $request->input('present_price') : '';
        $request->has('start_at') ? $data->discount_star_at = $request->input('start_at') : '';
        $request->has('end_at') ? $data->discount_end_at = $request->input('end_at') : '';
        $request->has('goods_number') ? $data->goods_number = $request->input('goods_number') : '';
        $data->status   = $request->input('status');
        $data->cover_id = $request->input('cover_id');
        $data->is_item  = $request->input('is_item');
        $request->has('is_index') ? $data->is_index = $request->input('is_index') : '';
        $request->has('top_recommended') ? $data->top_recommended = $request->input('top_recommended') : '';
        $request->has('show_place') ? $data->show_place = $request->input('show_place') : '';
        $request->has('index_cover_id') ? $data->index_cover_id = $request->input('index_cover_id') : '';
        $data->save();

        // 保存属性
        $attribute_id = $request->has('attribute_id') ? $request->input('attribute_id') : [];
        foreach ($request->input('attribute') as $key => $item) {
            if ($item && (strstr($item,'color') || strstr($item,'size'))) {
                $attribute           = $attribute_id[$key] ? GoodsAttribute::find($attribute_id[$key]) : new GoodsAttribute();
                $attribute->goods_id = $data->id;
                $attribute->value    = $item;
                $attribute->save();
            }
        }

        $picture_id      = $request->has('picture_id') ? $request->input('picture_id') : [];
        $sort            = $request->input('pic_sort_at');
        $picture_list_id = $request->input('picture_list_id');
        //保存图片
        foreach ($picture_id as $key => $item) {
            $picture             = $picture_list_id[$key] ? GoodsPicture::find($picture_list_id[$key]) : new GoodsPicture();
            $picture->goods_id   = $data->id;
            $picture->picture_id = $item;
            $sort[$key] ? $picture->sort_at = $sort[$key] : '';
            $picture->save();
        }

        // 加入缓存
        $data = Goods::find($data->id);
        Cache::put('wap_goods_info_' . $data->id, $data, 200);

        return redirect()->back()->withMessageSuccess('保存成功。');
    }

    /**
     * 删除图片
     */
    public function deletePicture(Request $request)
    {
        // 验证数据。
        $this->validate($request, [
            'id' => [
                'required',
                'exists:goods_pictures'
            ]
        ], [
            'id.required' => '请选择要删除的信息',
            'id.exists'   => '删除的信息不存在'
        ]);

        $data = GoodsPicture::find($request->input('id'))->delete();

        return response()->json([
            'status'  => 200,
            'message' => '删除成功'
        ]);
    }

    /**
     * 产品删除
     */
    public function delete(Request $request)
    {
        // 验证数据。
        $this->validate($request, [
            'id' => [
                'required',
                'exists:goods'
            ]
        ], [
            'id.required' => '请选择要删除的信息',
            'id.exists'   => '删除的信息不存在'
        ]);

        $data = Goods::find($request->input('id'))->delete();

        return redirect()->back()->withMessageSuccess('删除成功。');
    }

    /**
     * 产品评价列表
     */
    public function showEvaluateList(Request $request)
    {
        $data = GoodsEvaluate::where('goods_id', $request->input('id'))->paginate();

        return view('admin.goods.evaluate-list')->withData($data)->withGet($request->all());
    }

    /**
     * 产品评价编辑
     */
    public function showEvaluateEdit(Request $request)
    {
        $data = GoodsEvaluate::find($request->input('id'));

        return view('admin.goods.evaluate-edit')->withData($data)->withGet($request->all());
    }

    /**
     * 产品评价编辑保存
     */
    public function saveEvaluateEdit(Request $request)
    {
        // 验证数据。
        $this->validate($request, [
            'goods_info'  => [
                'required'
            ],
            'content'     => [
                'required'
            ],
            'evaluate_at' => [
                'required'
            ],
            'goods_id'    => [
                'required'
            ]
        ], [
            'goods_info.required'  => '请填写产品信息',
            'content.required'     => '请填写评价内容',
            'evaluate_at.required' => '请选择评价时间',
            'goods_id.required'    => '请选择产品'
        ]);
        $data              = $request->has('id') ? GoodsEvaluate::find($request->input('id')) : new GoodsEvaluate();
        $data->goods_info  = $request->input('goods_info');
        $data->content     = $request->input('content');
        $data->evaluate_at = $request->input('evaluate_at');
        $data->goods_id    = $request->input('goods_id');
        $data->order_id    = $request->has('order_id') ? $request->input('order_id') : uniqueid();
        $data->save();

        return redirect()->back()->withMessageSuccess('保存成功。');
    }

    /**
     * 产品评价删除
     */
    public function evaDelete(Request $request)
    {
        // 验证数据。
        $this->validate($request, [
            'id' => [
                'required',
                'exists:goods_evaluates'
            ]
        ], [
            'id.required' => '请选择要删除的信息',
            'id.exists'   => '删除的信息不存在'
        ]);
        $data = GoodsEvaluate::find($request->input('id'))->delete();

        return redirect()->back()->withMessageSuccess('删除成功。');
    }

    /**
     * 产品购买记录列表
     */
    public function showBuysList(Request $request)
    {
        $data = GoodsBuys::where('goods_id', $request->input('id'))->paginate();

        return view('admin.goods.buys-list')->withData($data)->withGet($request->all());
    }

    /**
     * 购买记录编辑
     */
    public function showBuysEdit(Request $request)
    {
        $data = GoodsBuys::find($request->input('id'));

        return view('admin.goods.buys-edit')->withData($data)->withGet($request->all());
    }

    /**
     * 购买记录保存
     */
    public function saveBuysEdit(Request $request)
    {
        // 验证数据。
        $this->validate($request, [
            'content'  => [
                'required'
            ],
            'goods_id' => [
                'required'
            ]
        ], [
            'content.required'  => '请填写内容',
            'goods_id.required' => '请选择产品'
        ]);
        $data           = $request->has('id') ? GoodsBuys::find($request->input('id')) : new GoodsBuys();
        $data->content  = $request->input('content');
        $data->goods_id = $request->input('goods_id');
        $data->save();

        return redirect()->back()->withMessageSuccess('保存成功。');
    }

    /**
     * 购买记录删除
     */
    public function delBuys(Request $request)
    {
        // 验证数据。
        $this->validate($request, [
            'id' => [
                'required',
            ]
        ], [
            'id.required' => '请选择要删除的信息',
        ]);
        $data = GoodsBuys::find($request->input('id'))->delete();

        return redirect()->back()->withMessageSuccess('删除成功。');
    }

}
